package com.hmall.api.fallback;

import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author chanchaw
 * @create 2025-08-08 14:30
 */
@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient(){
            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("feign客户端请求 queryItemByIds 查询商品信息出现异常：", cause);
                return Collections.emptyList();// 返回空集合
            }
            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                // 扣除库存是P0级错误，抛出给调用者，通知其失败的原因
                throw new RuntimeException(cause);
            }
        };
    }
}
